
package servlet;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import javax.servlet.jsp.JspException;
import util.NIO;

public class Parametros extends HttpServlet  {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            UtilWeb.incluir(request,response,"/cabecera.jsp");
            Enumeration<String> initParameterNames = this.getInitParameterNames();
            StringBuilder sb = new StringBuilder();
            sb.append("<table border='1'>");
            while(initParameterNames.hasMoreElements()){
                String nombre = initParameterNames.nextElement();
                String valor = this.getInitParameter(nombre);
                sb.append("<tr><td>").append(nombre).append("</td><td>")
                        .append(valor).append("</td></tr>");
            }
            sb.append("</table>");    

            Enumeration<String> globalInitParameterNames = this.getServletContext().getInitParameterNames();           
            sb.append("<table border='1'>");
            while(globalInitParameterNames.hasMoreElements()){
                String nombre = globalInitParameterNames.nextElement();
                String valor = this.getServletContext().getInitParameter(nombre);
                sb.append("<tr><td>").append(nombre).append("</td><td>")
                        .append(valor).append("</td></tr>");
            }
            sb.append("</table>");    

            Enumeration<String> parameterNames = request.getParameterNames();           
            sb.append("<table border='1'>");
            while(parameterNames.hasMoreElements()){
                String nombre = parameterNames.nextElement();
                String valor = "<b>"+request.getParameter(nombre)+"</b><br/>";
                String[] parameterValues = request.getParameterValues(nombre);
                for (String val : parameterValues) {
                    valor+=val+"<br/>";
                }
                sb.append("<tr><td>").append(nombre).append("</td><td>")
                        .append(valor).append("</td></tr>");
            }
            sb.append("</table>");                
            
            out.println(sb.toString());
            
            Part part = request.getPart("fichero");
//Content-Disposition: form-data; name="fichero"; filename="argouml.jar"
            String cadena = part.getHeader("Content-Disposition");
            String expresion = "[^;]+?;[^;]+?;\\s+filename=\"(.*?)\"";
            Pattern patron = Pattern.compile(expresion);
            Matcher m = patron.matcher(cadena);
            if(m.find()){
                String nombre = m.group(1);
                nombre = System.currentTimeMillis()+"_"+nombre;
                //part.write("c:\\"+nombre);
                InputStream is = part.getInputStream();
                FileOutputStream fos = new FileOutputStream("c:\\"+nombre);
                NIO.copiarFlujoAFlujo(part.getSize(), is, fos);
                fos.close();
            }
            
            
            UtilWeb.incluir(request,response,"/pie.jsp");
            
        } catch (JspException ex) {            
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
